package com.laikan.reader.core.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;

/**
 * Created by Administrator on 2016/10/28.
 */
public class TipInfoLayout extends FrameLayout {

    public static final int DATA_LOAD_FAIL = 0;
    public static final int DATA_LOAD_SUCCESS = 1;
    protected RelativeLayout common_loaddata_notice;
    private LinearLayout ll_empty_data;
    protected TextView common_prompt_content, common_refresh_net, tv_empty_data;
    private ImageView common_prompt_image, iv_empty_data;
    protected CircleProgressBar circleProgressBar;
    public long mLoadDelaynternalTime;
    private String errorTip;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case DATA_LOAD_FAIL:
                    changeShowPage(false, false);
                    break;
                default:
                    break;
            }
        }
    };

    public TipInfoLayout(Context context) {
        super(context);
        initView(context);
    }

    public TipInfoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TipInfoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.tip_info_layout, null, false);
        //请求异常
        common_loaddata_notice = (RelativeLayout)view.findViewById(R.id.common_loaddata_notice);
        common_prompt_image = (ImageView)view.findViewById(R.id.common_prompt_image);
        common_prompt_content = (TextView)view.findViewById(R.id.common_prompt_content);
        common_refresh_net = (TextView)view.findViewById(R.id.common_refresh_net);
        circleProgressBar = (CircleProgressBar)view.findViewById(R.id.comon_bgbar_loading);
        //空数据
        ll_empty_data = (LinearLayout)view.findViewById(R.id.ll_empty_data);
        iv_empty_data = (ImageView)view.findViewById(R.id.iv_empty_data);
        tv_empty_data = (TextView)view.findViewById(R.id.tv_empty_data);
        view.setOnClickListener(null);
        addView(view);
        this.setVisibility(View.GONE);
    }

    public void setOnClick(OnClickListener onClik) {
        common_refresh_net.setOnClickListener(onClik);
    }

    public void setLoading() {
        changeShowPage(false, true);
        mLoadDelaynternalTime = System.currentTimeMillis() + 2000;
    }

    public void setLoadError(int resId) {
        this.errorTip = LaikanApplication.getInstance().getResources().getString(resId);
        closeDelayLoading(System.currentTimeMillis());
    }

    public void setLoadErrorNotDelayed(int resId) {
        this.errorTip = LaikanApplication.getInstance().getResources().getString(resId);
        handler.sendEmptyMessage(DATA_LOAD_FAIL);
    }

    public void setEmptyData(int msgId, int resImgId) {
        String msg = LaikanApplication.getInstance().getResources().getString(msgId);
        changeShowPage(true, false);
        tv_empty_data.setText(msg);
        iv_empty_data.setImageResource(resImgId);
    }

    public void setHide() {
        this.setVisibility(View.GONE);
    }

    private void changeShowPage(boolean isEmptyData, boolean isLoading) {
        if (isEmptyData) {
            common_loaddata_notice.setVisibility(View.GONE);
            ll_empty_data.setVisibility(View.VISIBLE);
        }else {
            if (isLoading) {
                circleProgressBar.setVisibility(View.VISIBLE);
                common_prompt_content.setVisibility(View.GONE);
                common_refresh_net.setVisibility(View.GONE);
                common_prompt_image.setVisibility(View.GONE);
            }else {
                circleProgressBar.setVisibility(View.INVISIBLE);
                common_prompt_content.setVisibility(View.VISIBLE);
                common_refresh_net.setVisibility(View.VISIBLE);
                common_prompt_content.setText(errorTip);
                common_prompt_image.setVisibility(View.VISIBLE);
            }
            ll_empty_data.setVisibility(View.GONE);
            common_loaddata_notice.setVisibility(View.VISIBLE);
        }
        this.setVisibility(View.VISIBLE);
    }

    public void closeDelayLoading(long currenttime) {
        if (currenttime < mLoadDelaynternalTime) {
            handler.sendEmptyMessageDelayed(DATA_LOAD_FAIL, 1000);
        }else {
            handler.sendEmptyMessage(DATA_LOAD_FAIL);
        }
    }
}
